package com.wude.guild.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wude.bean.AppInfo;
import com.wude.guild.GameDetActivity;
import com.wude.guild.R;
import com.wude.guild.adapter.MyGameListAdapter;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.tools.DbUtils;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/11/23 0023.
 */

public class MyGameFragment extends TitleBaseFragment {
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.error_text)
    TextView errorText;
    @BindView(R.id.error_layout)
    RelativeLayout errorLayout;
    private MyGameListAdapter myGameListAdapter;
    private Unbinder unbinder;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_my_game, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MyGameFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MyGameFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);

        setHeaderTitle("我的游戏");
        initdata();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initdata() {
        myGameListAdapter = new MyGameListAdapter(getActivity());
        listview.setAdapter(myGameListAdapter);
        final List<AppInfo> list = getList();
        if (list != null && list.size() != 0) {
            listview.setVisibility(View.VISIBLE);
            errorLayout.setVisibility(View.GONE);
            myGameListAdapter.setList(list);
        } else {
            listview.setVisibility(View.GONE);
            errorLayout.setVisibility(View.VISIBLE);
            errorText.setText("未发现已安装游戏");
        }
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AppInfo appInfo = list.get(i);
                Intent intent = new Intent(getActivity(), GameDetActivity.class);
                intent.putExtra("id", appInfo.id + "");
                startActivity(intent);
            }
        });

    }

    public List<AppInfo> getList() {
        try {
            DbManager db = DbUtils.getDB();
            List<AppInfo> btnStatus = db.selector(AppInfo.class).where("Status", "=", 5).findAll();
            return btnStatus;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }
}
